package com.pig4cloud.pig.zhang.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.zhang.entity.WzArticle;
import com.pig4cloud.pig.zhang.service.WzArticleService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/wzArticle" )
public class WzArticleController {
	private final WzArticleService wzArticleService;

	@GetMapping("/page")
	public R getWzArticlePage(@ParameterObject Page page, @ParameterObject WzArticle wzArticle){
		return R.ok(wzArticleService.pageArticles(page, wzArticle));
	}

	@GetMapping("/{articleId}")
	public R getWzArticleById(@PathVariable Long articleId){
		return R.ok(wzArticleService.getById(articleId));
	}

	@PostMapping
	public R save(@RequestBody WzArticle wzArticle) {
		return R.ok(wzArticleService.save(wzArticle));
	}

	@PutMapping
	public R updateById(@RequestBody WzArticle wzArticle) {
		return R.ok(wzArticleService.updateById(wzArticle));
	}

	@DeleteMapping
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(wzArticleService.removeBatchByIds(CollUtil.toList(ids)));
	}
	/**
	 * 根据栏目ID查询文章列表
	 * @param columnId 栏目ID
	 * @return 文章列表
	 */
	@Operation(summary = "根据栏目ID查询文章列表" , description = "根据栏目ID查询文章列表" )
	@SysLog("根据栏目ID查询文章列表" )
	@GetMapping("/column/{columnId}")
		public List<WzArticle> getWzArticleByColumnId(@PathVariable("columnId" ) Long columnId){
		LambdaQueryWrapper<WzArticle> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(WzArticle::getColumnId, columnId);
		return wzArticleService.list(queryWrapper);
	}

	/**
	 * 发布/撤回文章
	 *
	 * @return 成功/失败
	 */
	@Operation(summary = "发布/撤回文章" , description = "发布/撤回文章" )
	@SysLog("发布/撤回文章" )
	@PutMapping("/release")
	public R releaseOrRevoke(@RequestBody WzArticle wzArticle) {
		if (wzArticle.getState().equals("0")){
			wzArticle.setState("1");
			return R.ok(wzArticleService.updateById(wzArticle));
		}else{
			if (wzArticle.getNoticeAnnouncement().equals("1")||
					wzArticle.getPhotoNews().equals("1")||
					wzArticle.getRecommend().equals("1")){
				return R.failed("该文章无法撤回");
			}else{
			wzArticle.setState("0");
			return R.ok(wzArticleService.updateById(wzArticle));
			}
		}
	}
	@Operation(summary = "根据栏目ID查询可公告的文章列表" , description = "根据栏目ID查询可公告的文章列表" )
	@SysLog("根据栏目ID查询可公告的文章列表" )
	@GetMapping("/na")
	public List<WzArticle> getWzArticleNAByColumnId(@RequestParam Long columnId,@RequestParam(required = false) String[] createTime){
		LambdaQueryWrapper<WzArticle> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(WzArticle::getColumnId, columnId).eq(WzArticle::getNoticeAnnouncement, "0");
		if (createTime!= null && createTime.length > 0){
			queryWrapper.between(WzArticle::getCreateTime, LocalDateTime.parse(createTime[0]), LocalDateTime.parse(createTime[1]));
		}
		return wzArticleService.list(queryWrapper);
	}
	@Operation(summary = "根据栏目ID查询可图片新闻的文章列表" , description = "根据栏目ID查询可图片新闻的文章列表" )
	@SysLog("根据栏目ID查询可图片新闻的文章列表" )
	@GetMapping("/pn")
	public List<WzArticle> getWzArticlePNByColumnId(@RequestParam Long columnId,@RequestParam(required = false) String[] createTime){
		LambdaQueryWrapper<WzArticle> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(WzArticle::getColumnId, columnId).eq(WzArticle::getPhotoNews, "0")
				.ne(WzArticle::getImage, "");
		if (createTime!= null && createTime.length > 0){
			queryWrapper.between(WzArticle::getCreateTime, LocalDateTime.parse(createTime[0]), LocalDateTime.parse(createTime[1]));
		}
		return wzArticleService.list(queryWrapper);
	}
}
